<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * 后台控制类
 */
class Admin extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();

        //admin下的每个方法都经过这个初始化方法，因此用来检测登录状态，保证后台安全
        if (!$this->session->userdata('admin') && $this->router->fetch_method() != 'login')
        {
            //如果没有登录 且 不在登录页面，则跳转到登录页面
            redirect(base_url('admin/login'), 'refresh');
            return;
        }
    }

    /**
     * 登录页面
     */
    public function login()
    {
        //如果已经登录，直接跳转入后台主页面
        if ($this->session->userdata('admin')) //检测session
        {
            redirect(base_url('admin/'), 'refresh');
        }
        //接收用户输入，post格式
        $username = $this->input->post('username', TRUE);
        $password = $this->input->post('password', TRUE);
        //判断账密
        if ($username && $password)
        {
            $userinfo = $this->admin_model->get_login_info_by_username($username);
            
            if ($userinfo && $userinfo['passwd'] == md5(md5(md5($password))) && $userinfo['role']=="admin")
            {
                $this->session->set_userdata('admin', $userinfo['role']); //设置session
                $this->session->set_userdata('admin_name', $userinfo['name']);
                redirect(base_url('admin/index'), 'refresh');
                return;
            }
            else
            {
                echo iconv("UTF-8","GBK","<script>alert('用户名或密码错误，请返回检查。'); history.go(-1);</script>");
                exit();
            }
        }
        $this->load->view('admin/login');
    }

    /**
     * 退出
     */
    public function logout()
    {
        $this->session->sess_destroy(); //清除session
        redirect(base_url('admin/login'), 'refresh');
    }

    /**
     * 没什么用的首页。。。
     */
    public function index()
    {
        $data['id'] = 'index';
        $this->load->view('admin/index',$data);
    }
    
    public function userList()
    {
        $data['work'] = $this->admin_model->get_user();
        $data['id'] = "user";
        $this->load->view('admin/userlist',$data);
    }
    
    public function update($role)
    {
        if ($role=='admin')
        {
            if ($this->admin_model->update_by_userName($role))
            {
                echo iconv("UTF-8","GBK","<script>alert('修改成功，请重新登录');</script>");
                redirect(base_url('admin/logout'), 'refresh');
                redirect(base_url('admin/'), 'refresh');
            }
            else
            {
                echo iconv("UTF-8","GBK","<script>alert('修改失败，请检查密码是否正确');history.go(-1);</script>");
            }
        }
        else
        {
            if ($this->admin_model->update_by_userName($role))
            {
                echo iconv("UTF-8","GBK","<script>alert('修改成功');</script>");
                redirect(base_url('admin/userlist'), 'refresh');
            }
            else
            {
                echo iconv("UTF-8","GBK","<script>alert('修改失败，请检查是否新旧密码一致');history.go(-1);</script>");
            }
        }
    }
    
    public function addUser()
    {
        if ($this->admin_model->addUser())
        {
            echo iconv("UTF-8","GBK","<script>alert('增加成功');</script>");
            redirect(base_url('admin/userlist'), 'refresh');
        }
        else
        {
            echo iconv("UTF-8","GBK","<script>alert('用户名已存在'); history.go(-1);</script>");
        }
    }
    
    public function changeUserView($do,$id=null)
    {
        if ($do!="admin")
        {
            if ($id==null)
            {
                $data['doIt'] = $do;
                $data['to'] = 'addUser';
                $this->load->view('admin/changeUser',$data);
            }
            else
            {
                $data['work']=  $this->admin_model->get_user_by_id($id);
                $data['doIt'] = $do;
                $data['to'] = 'update/noAdmin';
                $this->load->view('admin/changeUser',$data);
            }
        }
        else 
        {
            $data['id']="updatepw";
            $data['doIt']="admin";
            $data['to']="update/admin";
            $this->load->view('admin/changeUser',$data);
        }
    }
    
}